package com.deyuanyun.pic.insp.service.statistics.impl;

import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.common.util.ajax.AjaxSupport;
import com.deyuanyun.pic.common.util.web.Asserts;
import com.deyuanyun.pic.insp.mapper.AnalysisMapper;
import com.deyuanyun.pic.insp.service.inspresult.InspCommService;
import com.deyuanyun.pic.insp.service.inspresult.impl.BaseServiceImpl;
import com.deyuanyun.pic.insp.service.statistics.AnalysisService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 内检测-缺陷统计-对比分析 接口实现
 *
 * @author scalpel.王敏
 * @version [v1.0] 创建时间（2016/6/24）
 * @see // 引用的类
 * Method List:
 * 1.----------------queryData 查询内检测数据和原始数据
 * 2.----------------queryInspBar 查询内检测数据
 * 3.----------------queryPinspBar 查询原始数据
 * History:
 */

@Service
public class AnalysisServiceImpl implements AnalysisService {


    @Resource
    private AnalysisMapper analysisMapper;

    @Resource
    private InspCommService inspCommService;

    /**
     * 查询对比分析数据
     * @param param 查询条件
     *
     * @return
     */
    @Override
    public Map<String, Object> queryData(Map<String, Object> param) {
        Map<String ,Object> result=new HashMap<String,Object>();
        List<Map<String,Object>> taskList= inspCommService.getNewTask(param);
        if(taskList.size() != 2)
        {
            AjaxSupport.sendSuccessText("false","没有可比较的检测信息");
            return null;
        }


        //设置绝对距离
        {


            BigDecimal beginMileage = (BigDecimal) taskList.get(0).get("BEGINMILEAGE");
            BigDecimal endMileage = (BigDecimal) taskList.get(0).get("ENDMILEAGE");

            if(StringUtils.isEmpty((String)param.get("beginMileage")))
            {
                param.put("beginMileage",beginMileage.doubleValue()+"");
                param.put("endMileage",endMileage.doubleValue()+"");
            }
            BaseServiceImpl.cheackMileage(param,beginMileage,endMileage);
        }

        //第一次检测任务 缺陷信息查询
        param.put("taskId",taskList.get(0).get("ID"));
        result.put("twoBar",queryBar(param));
        result.put("two",analysisMapper.query(param));
        result.put("twoDate", taskList.get(0).get("DDATE"));

        //第二次检测任务 缺陷信息查询
        param.put("taskId",taskList.get(1).get("ID"));
        result.put("oneBar",queryBar(param));
        result.put("one",analysisMapper.query(param));
        result.put("oneDate",taskList.get(1).get("DDATE"));



        String taskIdOne= (String) param.get("taskId1");
        String taskId= (String) taskList.get(0).get("ID");
        //第一次检测任务 缺陷信息查询
        param.put("taskId",taskId);

        setResult(result,param,taskId.equals(taskIdOne)?"one":"two",taskList.get(0).get("DDATE"));

        //第二次检测任务 缺陷信息查询
        taskId= (String) taskList.get(1).get("ID");
        if(taskIdOne == null)
        {
            taskIdOne = taskId ;
        }
        param.put("taskId",taskId);
        setResult(result,param,taskId.equals(taskIdOne)?"one":"two",taskList.get(1).get("DDATE"));

        return result;
    }

    /**
     * 对比分析的柱状图查询，name：one 第一次检测任务的柱状图
     *                             two 第二次检测任务的柱状图
     * @param result
     * @param param
     * @param name
     * @param date
     */
    public void setResult( Map<String ,Object> result,Map<String, Object> param,String name,Object date)
    {
        result.put(name+"Bar",queryBar(param));
        result.put(name,analysisMapper.query(param));
        result.put(name+"Date",date);
    }

    private Map<String, Object> queryBar(Map<String, Object> param) {
        Map<String,Object> result=new HashMap<String,Object>();


        BigDecimal beginMileage=null;
        BigDecimal lengthMileage= null;

        if( null == param.get("beginMileage") )
        {
            //查询管道或者管段的里程数，以及起始位置的绝对里程
            Map<String,Object> mileage=inspCommService.queryLineMileage(param);
            beginMileage=(BigDecimal) mileage.get("BEGINMILEAGE");
            lengthMileage=(BigDecimal) mileage.get("LENGTHMILEAGE");
        }else{
            beginMileage=BigDecimal.valueOf((Double)param.get("beginMileage"));
            lengthMileage=BigDecimal.valueOf((Double)param.get("endMileage"));
        }



        List<Map<String,Object>> data=new ArrayList<Map<String,Object>>();
        Map<String,Object> info=null;

        BigDecimal df=lengthMileage.subtract(beginMileage).divide(BigDecimal.valueOf(10));
        for(int i=0;i<10;i++)
        {

            info=new HashMap<String,Object>();
            info.put("name","第"+(i+1)+"段");
            info.put("end",false);
            info.put("b",beginMileage.add(df.multiply(BigDecimal.valueOf(i))));
            info.put("e",beginMileage.add(df.multiply(BigDecimal.valueOf(i+1))));
            if(i == 9)
            {
                info.put("end","true");
            }

            data.add(info);
        }
        param.put("data",data);


        //查询内部金属损失
        param.put("type","1");

        result.put("nbjs",analysisMapper.queryBar(param));
        //查询外部金属损失
        param.put("type","2");
        result.put("wbjs",analysisMapper.queryBar(param));

        //查询凹陷
        param.put("type","3");
        result.put("ax",analysisMapper.queryBar(param));

        //查询焊缝缺陷
        param.put("type","4");
        result.put("hf",analysisMapper.queryBar(param));

        //查询螺旋焊缝
        param.put("type","5");
        result.put("lxhf",analysisMapper.queryBar(param));

        return result;
    }
}
